package com.study.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.study.dao.OrderSettingDao;
import com.study.pojo.OrderSetting;
import com.study.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public void add(List<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            Long count = orderSettingDao.findCountByTime(orderSetting.getOrderDate());
            if(count > 0){
                orderSettingDao.updateByTime(orderSetting);
            }else {
                orderSettingDao.add(orderSetting);
            }
        }
    }

    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String date) {
        String dateStar = date.concat("-1");
        String dateEnd = date.concat("-31");
        Map<String, String> map = new HashMap<>();
        map.put("dateStar",dateStar);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);
        List<Map<String, Object>> list = new ArrayList<>();
        if(orderSettingList != null && orderSettingList.size() >0){
            for (OrderSetting orderSetting : orderSettingList) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("date",orderSetting.getOrderDate().getDate());
                hashMap.put("number",orderSetting.getNumber());
                hashMap.put("reservations",orderSetting.getReservations());
                list.add(hashMap);
            }
        }
        return list;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Long count = orderSettingDao.findCountByTime(orderSetting.getOrderDate());
        if(count > 0){
            orderSettingDao.updateByTime(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
